{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Const_5:0\", shape=(), dtype=int32)\n",
      "<tf.Variable 'var_5:0' shape=() dtype=int32_ref>\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "## opencv tensorflow\n",
    "## 类比 语法 api  原理\n",
    "## 基础数据类型  运算符 流程 字典 数组\n",
    "import tensorflow as tf\n",
    "data1 = tf.constant(3,dtype=tf.int32)\n",
    "data2 = tf.Variable(10,name='var')\n",
    "print(data1)\n",
    "print(data2)\n",
    "'''\n",
    "sess = tf.Session()\n",
    "print(sess.run(data1))\n",
    "init = tf.global_variables_initializer()\n",
    "sess.run(init)\n",
    "print(sess.run(data2))\n",
    "sess.close()\n",
    "'''\n",
    "# 本质 tf = tensor + 计算图\n",
    "# tensor 数据\n",
    "# op \n",
    "# graphs 数据操作\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "sess = tf.Session()\n",
    "with sess:\n",
    "    sess.run(init)\n",
    "    print(sess.run(data2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "12\n",
      "4\n",
      "3.0\n",
      "end!\n"
     ]
    }
   ],
   "source": [
    "## 常量的四则运算\n",
    "data1 = tf.constant(6)\n",
    "data2 = tf.constant(2)\n",
    "dataAdd = tf.add(data1, data2)\n",
    "dataMul = tf.multiply(data1, data2)\n",
    "dataSub = tf.subtract(data1, data2)\n",
    "dataDiv = tf.divide(data1, data2)\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(dataAdd))\n",
    "    print(sess.run(dataMul))\n",
    "    print(sess.run(dataSub))\n",
    "    print(sess.run(dataDiv))\n",
    "print(\"end!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "8\n",
      "12\n",
      "4\n",
      "3.0\n",
      "end!\n"
     ]
    }
   ],
   "source": [
    "## 变量的四则运算\n",
    "data1 = tf.constant(6)\n",
    "data2 = tf.Variable(2)\n",
    "dataAdd = tf.add(data1, data2)\n",
    "dataMul = tf.multiply(data1, data2)\n",
    "dataSub = tf.subtract(data1, data2)\n",
    "dataDiv = tf.divide(data1, data2)\n",
    "init = tf.global_variables_initializer()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    print(sess.run(dataAdd))\n",
    "    print(sess.run(dataMul))\n",
    "    print(sess.run(dataSub))\n",
    "    print(sess.run(dataDiv))\n",
    "print(\"end!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "12\n",
      "4\n",
      "3.0\n",
      "sess.run(dataCopy) 8\n",
      "dataCopy.eval() 14\n",
      "tf.get_default_session().run(dataCopy) 20\n",
      "end!\n"
     ]
    }
   ],
   "source": [
    "data1 = tf.constant(6)\n",
    "data2 = tf.Variable(2)\n",
    "dataAdd = tf.add(data1, data2)\n",
    "dataCopy = tf.assign(data2,dataAdd)  #dataAdd -> data2\n",
    "dataMul = tf.multiply(data1, data2)\n",
    "dataSub = tf.subtract(data1, data2)\n",
    "dataDiv = tf.divide(data1, data2)\n",
    "init = tf.global_variables_initializer()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    print(sess.run(dataAdd))\n",
    "    print(sess.run(dataMul))\n",
    "    print(sess.run(dataSub))\n",
    "    print(sess.run(dataDiv))\n",
    "    print('sess.run(dataCopy)',sess.run(dataCopy))  # dataAdd -> data2\n",
    "    print('dataCopy.eval()',dataCopy.eval())   # 此时data2 = 8, dataCopy = data2 = dataAdd = 8+6 = 14 \n",
    "    print('tf.get_default_session().run(dataCopy)',tf.get_default_session().run(dataCopy))  # 此时data2 = 14, dataCopy = data2 = dataAdd = 14 + 6 = 20\n",
    "print(\"end!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
